package view.rule.organizer;

import java.io.File;

import javax.swing.DropMode;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

public class RuleTree extends JScrollPane{
	
	JTree tree;
	DefaultMutableTreeNode root = new DefaultMutableTreeNode("Rules");
	File folder = new File("Rules");
	
	public RuleTree(){
		getFiles(folder, root);
        addComponents();
        
        this.setViewportView(tree);
        this.setVisible(true);
	}
	
	public void getFiles(File folder, DefaultMutableTreeNode root){
	    for (int i = 0; i < folder.listFiles().length; i++) {
	      if (folder.listFiles()[i].isFile()) {
	    	  root.add(new DefaultMutableTreeNode(folder.listFiles()[i].getName()));
	      }
	      else if (folder.listFiles()[i].isDirectory()){
	    	  setDirectory(folder.listFiles()[i]);
	    	  System.out.println(folder.listFiles()[i]);
	      }
	    }
	}
	
	public void setDirectory(File folder){
		DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode(folder.getName());
		getFiles(folder, newRoot);
		root.add(newRoot);
	}
	
	public void addComponents(){
        tree = new JTree(root);
        tree.setDragEnabled(true);
        tree.setDropMode(DropMode.ON_OR_INSERT);
        tree.setTransferHandler(new TreeTransferHandler());
        tree.getSelectionModel().setSelectionMode(
                TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
        
	}
}
